package com.kinsin.kinsinutil

import android.content.Context
import android.content.res.Resources
import kotlin.math.roundToInt

/**
 * 尺寸工具类
 * @author kinsin
 */
object DimenUtils {

    /**
     * 将dp值转换为像素（支持浮点运算）
     * 添加对density=0的保护逻辑
     */
    fun Int.dpToPx(resources: Resources): Int {
        val density = resources.displayMetrics.density
        if (density == 0f) return 0
        return (this * density).roundToInt()
    }

    fun Float.dpToPx(resources: Resources): Int {
        val density = resources.displayMetrics.density
        if (density == 0f) return 0
        return (this * density).roundToInt()
    }

    /**
     * 将像素值转换为dp（支持浮点运算）
     * 添加对density=0的保护逻辑
     */
    fun Int.pxToDp(resources: Resources): Int {
        val density = resources.displayMetrics.density
        if (density == 0f) return 0
        return (this / density).roundToInt()
    }

    fun Float.pxToDp(resources: Resources): Int {
        val density = resources.displayMetrics.density
        if (density == 0f) return 0
        return (this / density).roundToInt()
    }

    /**
     * 将sp值转换为像素（考虑系统字体缩放）
     * 添加对scaledDensity=0的保护逻辑
     */
    fun Int.spToPx(resources: Resources): Int {
        val scaledDensity = resources.displayMetrics.scaledDensity
        if (scaledDensity == 0f) return 0
        return (this * scaledDensity).roundToInt()
    }

    fun Float.spToPx(resources: Resources): Int {
        val scaledDensity = resources.displayMetrics.scaledDensity
        if (scaledDensity == 0f) return 0
        return (this * scaledDensity).roundToInt()
    }

    /**
     * 将像素值转换为sp
     * 添加对scaledDensity=0的保护逻辑
     */
    fun Int.pxToSp(resources: Resources): Int {
        val scaledDensity = resources.displayMetrics.scaledDensity
        if (scaledDensity == 0f) return 0
        return (this / scaledDensity).roundToInt()
    }

    fun Float.pxToSp(resources: Resources): Int {
        val scaledDensity = resources.displayMetrics.scaledDensity
        if (scaledDensity == 0f) return 0
        return (this / scaledDensity).roundToInt()
    }

    /**
     * 获取屏幕物理尺寸
     */
    fun getScreenWidth(resources: Resources): Int =
        resources.displayMetrics.widthPixels

    fun getScreenHeight(resources: Resources): Int =
        resources.displayMetrics.heightPixels

    /* Context扩展版本 */
    fun Int.dpToPx(context: Context): Int = dpToPx(context.resources)
    fun Float.dpToPx(context: Context): Int = dpToPx(context.resources)
    fun Int.pxToDp(context: Context): Int = pxToDp(context.resources)
    fun Float.pxToDp(context: Context): Int = pxToDp(context.resources)
    fun Int.spToPx(context: Context): Int = spToPx(context.resources)
    fun Float.spToPx(context: Context): Int = spToPx(context.resources)
    fun Int.pxToSp(context: Context): Int = pxToSp(context.resources)
    fun Float.pxToSp(context: Context): Int = pxToSp(context.resources)
    fun getScreenWidth(context: Context): Int = getScreenWidth(context.resources)
    fun getScreenHeight(context: Context): Int = getScreenHeight(context.resources)
}